﻿using Lmondeil.Crm.EF;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Atlanchim.Crm.Mui.Pages.Contacts
{
    public class PageEditContactModel : AbstractPageEditModel
    {
        public Contact Contact { get; set; }

        public override void InnerLoad()
        {
            if (!string.IsNullOrEmpty(this.Fragment))
            {
                // Récupération des Ids
                int idContact = 0;
                int.TryParse(this.DicFragments["IdContact"], out idContact);

                int idSociete = 0;
                if (this.DicFragments.ContainsKey("IdSociete"))
                {
                    int.TryParse(this.DicFragments["IdSociete"], out idSociete);
                }

                // Chargement ou création de l'entité
                if (idContact > 0)
                {
                    this.Contact = this._pageContext
                                        .Contacts
                                        .Where(contact => contact.Id == idContact)
                                        .FirstOrDefault();
                }
                else
                {
                    this.Contact = new Contact();
                    this.Contact.IdSociete = idSociete;
                    this._pageContext.Contacts.Add(this.Contact);
                }
            }
        }
    }
}
